package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;

import android.app.FragmentManager;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //private List<Map<String,Object>> data = new ArrayList<>();

    private Fragment wechatFragment = new wechatFragment();
    private Fragment friendFragment = new friendFragment();
    private Fragment contactFragment = new contactFragment();
    private Fragment configFragment = new configFragment();

    private ImageView ImageView1;
    private ImageView ImageView2;
    private ImageView ImageView3;
    private ImageView ImageView4;

    //private Context context;

    private FragmentManager fragmentManager;
    private LinearLayout linearLayout1, linearLayout2, linearLayout3, linearLayout4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        linearLayout1 = findViewById(R.id.LinearLayout_wechat);
        linearLayout2 = findViewById(R.id.LinearLayout_friend);
        linearLayout3 = findViewById(R.id.LinearLayout_contact);
        linearLayout4 = findViewById(R.id.LinearLayout_config);


        // 底部菜单4个ImageView
        ImageView1 = findViewById(R.id.imageView1);
        ImageView2 = findViewById(R.id.imageView7);
        ImageView3 = findViewById(R.id.imageView3);
        ImageView4 = findViewById(R.id.imageView4);


        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);

        //MyAdapter adapter = new MyAdapter(data, this);

        //LinearLayoutManager manager=new LinearLayoutManager(context);

        initFragment();
        showFragment(0);
    }


    //没有用的图标都变灰
    public void reseting(){
        ImageView1.setImageResource(R.drawable.weixin);
        ImageView2.setImageResource(R.drawable.friend);
        ImageView3.setImageResource(R.drawable.contact);
        ImageView4.setImageResource(R.drawable.config);
    }




    private void initFragment() {
        fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        transaction.add(R.id.id_content, wechatFragment);
        transaction.add(R.id.id_content, friendFragment);
        transaction.add(R.id.id_content, contactFragment);
        transaction.add(R.id.id_content, configFragment);
        transaction.commit();

    }

    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(wechatFragment);
        transaction.hide(friendFragment);
        transaction.hide(contactFragment);
        transaction.hide(configFragment);

    }

    @Override
    public void onClick(View view) {
        reseting();
        switch (view.getId()) {
            case R.id.LinearLayout_wechat:
                showFragment(0);
                break;
            case R.id.LinearLayout_friend:
                showFragment(1);
                break;
            case R.id.LinearLayout_contact:
                showFragment(2);
                break;
            case R.id.LinearLayout_config:
                showFragment(3);
                break;
        }

    }

    private void showFragment(int i) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                transaction.show(wechatFragment);
                ImageView1.setImageResource(R.drawable.weixin1);
                break;
            case 1:
                transaction.show(friendFragment);
                ImageView2.setImageResource(R.drawable.friend1);
                break;
            case 2:
                transaction.show(contactFragment);
                ImageView3.setImageResource(R.drawable.contact1);
                break;
            case 3:
                transaction.show(configFragment);
                ImageView4.setImageResource(R.drawable.config1);
                break;
            default:
                break;
        }
        transaction.commit();
    }


}